import localforage from 'localforage'

let user = {
  userId: null,
  openId: null,
  nickname: '',
  couponCount: 0,
  boxCount: 0,
  avatar: null
}

// let localUser = {
//   userId: 'ee3ba937-00e4-4174-9c03-bcec572eab2c',
//   openId: 'olKiy1WFtUt_YiLTW_CzZyl7DAt4',
//   nickname: 'czq',
//   couponCount: 4,
//   boxCount: 1,
//   avatar: 'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83epeBnlQVKqKOoQjD4k3cr5hiaV1kvK4MAyGzdSaicMSBEnicXGLxxUGcCSzabalO0m0OhWWIPDHPHVZA/0'
// }

let userInfo = (callBack) => {
  // if (localUser.userId) {
    // callBack(localUser)
  if (user.userId) {
    callBack(user)
  } else {
    localforage.getItem('userInfo', (err, value) => {
      if (!err) {
        callBack(value)
      } else {
        callBack(null)
      }
    })
  }
}

let saveUserInfo = userInfo => {
  user = userInfo
  return user
}

module.exports = {
  userInfo,
  saveUserInfo
}
